﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace BlueLotus7.Common
{
    public class EncodingUtility
    {
        private static EncodingUtility instance = new EncodingUtility();
        public static EncodingUtility Instance
        {
            get
            {
                return instance;
            }
        }

        private static Random random = new Random();
        private EncodingUtility() { }

        private char[] alpha = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

        public string GetRnd_NumOnly(int length)
        {
            StringBuilder sb = new StringBuilder();
            sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(alpha[random.Next(10)]);
            }
            return sb.ToString();
        }

        public string GetRndHexString(int length)
        {

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(alpha[random.Next(0, 16)]);
            }
            return sb.ToString();
        }

        public byte[] GetRndHex(int length)
        {
            byte[] result = new byte[length];

            for (int i = 0; i < length; i++)
            {
                StringBuilder sb = new StringBuilder();

                sb.Append(alpha[random.Next(0, 16)]);
                sb.Append(alpha[random.Next(0, 16)]);

                result[i] = Convert.ToByte(sb.ToString(), 16);
            }
            return result;
        }

        public string GetRnd_Full(int length)
        {

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                sb.Append(alpha[random.Next(36)]);
            }
            return sb.ToString();

        }

        public string EncodeStringToMD5(string str)
        {
            byte[] data = Encoding.Default.GetBytes(str);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] OutBytes = md5.ComputeHash(data);

            string OutString = "";
            for (int i = 0; i < OutBytes.Length; i++)
            {
                OutString += OutBytes[i].ToString("x2");
            }
            return OutString.ToUpper();
        }
        public string EncodeFileToMD5(string path)
        {
            try
            {
                FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                MD5CryptoServiceProvider get_md5 = new MD5CryptoServiceProvider();
                byte[] hash_byte = get_md5.ComputeHash(get_file);
                string resule = System.BitConverter.ToString(hash_byte);
                resule = resule.Replace("-", "");
                return resule.ToUpper();
            }
            catch (System.Exception e) { return e.ToString(); }
        }

        public string EncodingToSHA1(string sourceString, Encoding encoding)
        {
            byte[] StrRes = encoding.GetBytes(sourceString);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }

        public string EncodingToSHA1(string sourceString)
        {
            return EncodingToSHA1(sourceString, Encoding.Default);
        }


        #region Base64

        public string EncodeBase64FromString(string source)
        {
            try
            {
                byte[] bytes = System.Text.Encoding.Default.GetBytes(source);
                return Convert.ToBase64String(bytes);
            }
            catch
            {
                return string.Empty;
            }
        }

        public string EncodeBase64FromBytes(byte[] bytes)
        {
            try
            {
                return Convert.ToBase64String(bytes);
            }
            catch
            {
                return string.Empty;
            }
        }

        public string DecodeBase64ToString(string result)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(result);
                return System.Text.Encoding.Default.GetString(bytes);
            }
            catch
            {
                return string.Empty;
            }
        }

        public byte[] DecodeBase64ToBytes(string result)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String(result);
                return bytes;
            }
            catch
            {
                return null;
            }
        }

        #endregion

        public string EncodeStringToUnicode(string srcText)
        {
            return System.Web.HttpUtility.UrlEncode(srcText);
        }

        public string DecodeUnicodeToString(string srcText)
        {
            return System.Web.HttpUtility.UrlDecode(srcText);
        }

        public string GenerateUploadFileName(int suffixRndLength)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00"));
            sb.Append(DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00"));
            sb.Append(DateTime.Now.Millisecond.ToString("000"));
            sb.Append(GetRnd_Full(suffixRndLength));
            return sb.ToString();
        }
    }
}
